//
//  RHDeviceTotalDataCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/27.
//

#import "RHDeviceTotalDataCell.h"
#import "RHDeviceCircleView.h"
#import "RHPowerStatusView.h"
@interface RHDeviceTotalDataCell()
@property (nonatomic ,strong) RHPowerStatusView *powerView;

@property(nonatomic,strong)QMUIButton *alarmBtn;
@property(nonatomic,strong)QMUIButton *faultBtn;
@property(nonatomic,strong)QMUIButton *firmwareBtn;
@property(nonatomic,strong)QMUILabel *firmwareLab;
@end


@implementation RHDeviceTotalDataCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setDeviceData:(RHDeviceDataModel *)deviceData
{
    _deviceData = deviceData;
    [self.faultBtn setTitle:[NSString stringWithFormat:@"%ld",_deviceData.fault] forState:UIControlStateNormal];
    [self.alarmBtn setTitle:[NSString stringWithFormat:@"%ld",_deviceData.alarm] forState:UIControlStateNormal];
    [self.firmwareBtn setTitle:deviceData.softVersion forState:UIControlStateNormal];
}

-(void)setTopoModel:(RHTopoModel *)topoModel
{
    _topoModel = topoModel;
    
    self.powerView.topoModel = topoModel;
    
}

-(void)setFirewareVersion:(NSString *)version
{
    self.firmwareLab.text = version;
}

- (void)setupUI{
    
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView.layer setCornerRadius:8];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    _powerView = [[RHPowerStatusView alloc] init];
    _powerView.imgSize = kScaleW(50);
    [bgView addSubview:_powerView];
    [_powerView hidePowerAndVoltage];
   
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    
    [_powerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.equalTo(lineV.mas_top).offset(-5);
        make.top.mas_offset(30);
        make.height.offset(kScaleW(220));
    }];
    
    QMUIButton *alarmBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [alarmBtn setImage:[UIImage imageNamed:@"device_alarm"] forState:UIControlStateNormal];
    [alarmBtn setTitleColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    [alarmBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [alarmBtn setImagePosition:QMUIButtonImagePositionLeft];
    [alarmBtn setSpacingBetweenImageAndTitle:5];
    [alarmBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [alarmBtn setTitle:@"0" forState:UIControlStateNormal];
    [alarmBtn setBackgroundColor:CFTabViewBgColor];
    [alarmBtn setFrame:CGRectMake(0, 0, 60, 30)];
    [alarmBtn round:15 RectCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft];
    [bgView addSubview:alarmBtn];
    self.alarmBtn = alarmBtn;
    
    QMUIButton *faultBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [faultBtn setImage:[UIImage imageNamed:@"device_fault"] forState:UIControlStateNormal];
    [faultBtn setTitleColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    [faultBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [faultBtn setImagePosition:QMUIButtonImagePositionLeft];
    [faultBtn setSpacingBetweenImageAndTitle:5];
    [faultBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [faultBtn setTitle:@"0" forState:UIControlStateNormal];
    [faultBtn setBackgroundColor:CFTabViewBgColor];
    [faultBtn setFrame:CGRectMake(0, 0, 60, 30)];
    [faultBtn round:15 RectCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft];
    [bgView addSubview:faultBtn];
    self.faultBtn = faultBtn;

    
    UIImageView *iconImageV = [[UIImageView alloc] init];
    [iconImageV setImage:[UIImage imageNamed:@"device_firmware_icon"]];
    [bgView addSubview:iconImageV];
    
    QMUILabel *titleLab = [[QMUILabel alloc] init];
    [titleLab setTextAlignment:NSTextAlignmentLeft];
    [titleLab setFont:[UIFont systemFontOfSize:15]];
    [titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [titleLab setText:@"固件版本".rh_localized];
    [bgView addSubview:titleLab];
    
    QMUIButton *contentBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [contentBtn setTitleColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.5] forState:UIControlStateNormal];
    [contentBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [contentBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [contentBtn setTitle:@"Ems_Firmware_v102" forState:UIControlStateNormal];
    [bgView addSubview:contentBtn];
    self.firmwareBtn = contentBtn;
    
    UIImageView *arrowV = [[UIImageView alloc] init];
    [arrowV setImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:arrowV];
    
    _firmwareLab = [[QMUILabel alloc] init];
    [_firmwareLab setTextAlignment:NSTextAlignmentRight];
    _firmwareLab.numberOfLines = 0;
    [_firmwareLab setFont:[UIFont systemFontOfSize:12]];
    [_firmwareLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_firmwareLab];
    
    kWeakSelf;
    [alarmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.right.mas_offset(0);
        make.top.mas_offset(15);
    }];
    
    [faultBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.right.mas_offset(0);
        make.top.equalTo(alarmBtn.mas_bottom).offset(10);
    }];
    
    [iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.left.mas_offset(15);
        make.top.equalTo(lineV.mas_bottom).offset(12);
    }];
    
    [titleLab sizeToFit];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageV.mas_right).offset(5);
        make.top.bottom.equalTo(iconImageV);
        make.width.mas_equalTo(titleLab.width+1);
    }];
    
    [contentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(titleLab);
        make.centerX.equalTo(titleLab);
        make.left.equalTo(titleLab.mas_right).offset(10);
        make.right.equalTo(arrowV.mas_left).offset(0);
    }];
    
    [arrowV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.mas_offset(-15);
        make.centerY.equalTo(titleLab);
    }];
    
    [_firmwareLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineV.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
        make.left.equalTo(titleLab.mas_right).offset(5);
        make.right.equalTo(arrowV.mas_left).offset(-5);
        make.height.greaterThanOrEqualTo(@40);
    }];
    
    
    [[alarmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.alarmActionBlock){
            weakSelf.alarmActionBlock();
        }
    }];
    
    [[faultBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.faultActionBlock){
            weakSelf.faultActionBlock();
        }
    }];
    
    [[contentBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.firmwareActionBlock){
            weakSelf.firmwareActionBlock();
        }
    }];
}



@end
